/*
	NAME : RUSHIKESH MUKUND FANSE
	ROLL NO : 16
	DIV :H
	ASSIGNMENT : GO BACK TO N
*/
#include<cstdio>
#include<iostream>
#include<vector>
#include<cstring>
#include<string>
#include<cstdlib>
#include<sstream>
using namespace std;
int push;
int last_push;
class gobackton{
	private:
	vector<int>v;
	int a[20];
	int windowsize;

	string str;
	stringstream ss;
	int temp;
	int original_data;
	int frame_to_corrupt;
	int data_got_corrupted;
	int l;
	public:
	gobackton()
	{
			l=0;
			data_got_corrupted=0;
			str="";
			push=0;
			last_push=0;
			windowsize=5;
			memset(a,'0',sizeof(int)*20);
			generate_data();
	}
	void generate_data()
	{
			for(int i=0;i<20;i++)
			{
					a[i]=i;
			}
	}
	int get_last_push()
	{
		return last_push;	
	}
	void sender()
	{
		printf("window of frames sent to reciever..............\n");
		for(l=0;l<windowsize&&last_push<20;l++)
		{
			v.push_back(a[push+l]);
			last_push++;
		}
			push=push+(last_push - push);
	}
	void error(int choice)
	{
		switch(choice)
		{
				case 1:
				break;
				case 2:
					frame_to_corrupt=rand()%5;
					original_data=v.at(frame_to_corrupt);
					v.at(frame_to_corrupt)=-1;
				break;
		}
	}
	void reciever(int choice)
	{
		switch(choice)
		{
			case 1:
				/*for(int i=0;i<v.size();i++a)
				{
					printf("frame %d recieved by reciever.............\n",v.at(i));
				}
				data_got_corrupted=0;*/
				printf("window containing frame from %d to %d is recieved.........\n ",push-5,push);
				if(v.size()!=20)
					printf("sends the ackcowlegement that the next expected frame is %d.............\n",push+1);
			break;
			case 2:
				for(int i=0;i<v.size();i++)
				{
					if(v.at(i)!=-1)
						printf("frame %d recieved by reciever.............\n",v.at(i));	
					else
					{
						printf("error in frame.......... \n");
						printf("sending acknowlegement\n");
							printf("acknowlegement \
									\n1.lost due to time out or disturbance\
									\n2.recieved by sender\n");
									cin>>choice;
						switch(choice)
						{
							case 1:
							/*	printf("resending the negative acknolegement ...........\n");
								printf("negative acknowlegement recieved by sender..............\n");
								printf("all incoming packets are droped for the given window\n");*/
								printf("resending the window containing frame from %d to %d.........\n ",push-5,push);
								printf("window recieved sucessfully...............\n");
								if(v.size()!=20)
									printf("sends the ackcowlegement that the next expected frame is %d.............\n",push+1);
								i=5;
								data_got_corrupted=0;	
							break;
							case 2:
								if(v.size()!=20)
									printf("sends the acknowlegement that the next expected frame is %d..............\n",original_data);
							break;
						}
						last_push=last_push-(v.size()-i);
						push=original_data;
						break;
					}
				}
				data_got_corrupted=1;
			break;

		}
	/*
		if(data_got_corrupted!=1)
		{
			printf("windowsize of packets successfully recieved by reciever.............\n");
				printf("positive acknowlegement sent to request next window\n");
				printf("positive acknowlegement \
						\n1.not reached due to disturbance or time out\
						\n2.recieved\n");
				cin>>choice;
				switch(choice)
				{
					case 1:
						printf("resending the positive acknolegement...........\n");
						printf("positive acknowlegement recieved by sender..............\n");
					break;
					case 2:
						printf("positive acknowlegement recieved by sender..............\n");
					break;
				}
		
		}*/
		v.clear();
	}
	
};
int main(int argc, char const *argv[])
{
	int choice;
	gobackton s;
	int window_to_be_sent=4;
	while(s.get_last_push()<20)
	{
		s.sender();
		printf("error introduced\n1.NO ERROR\n2.ERROR introduced\n");
		scanf("%d",&choice);
		s.error(choice);
		printf("window recieved at reciever side\n");
		s.reciever(choice);
	}
	printf("\n...............END OF TRANSMISSION...............\n");
	return 0;
}
/*
OUTPUT:
rishi@rishi-PC:~$ g++ gobackton.cpp 
rishi@rishi-PC:~$ ./a.out
window of frames sent to reciever..............
error introduced
1.NO ERROR
2.ERROR introduced
1
window recieved at reciever side
window containing frame from 0 to 5 is recieved.........
 sends the ackcowlegement that the next expected frame is 6.............
window of frames sent to reciever..............
error introduced
1.NO ERROR
2.ERROR introduced
2
window recieved at reciever side
frame 5 recieved by reciever.............
frame 6 recieved by reciever.............
frame 7 recieved by reciever.............
error in frame.......... 
sending acknowlegement
acknowlegement 									
1.lost due to time out or disturbance									
2.recieved by sender
1
resending the window containing frame from 5 to 10.........
 window recieved sucessfully...............
sends the ackcowlegement that the next expected frame is 11.............
window of frames sent to reciever..............
error introduced
1.NO ERROR
2.ERROR introduced
2
window recieved at reciever side
frame 8 recieved by reciever.............
error in frame.......... 
sending acknowlegement
acknowlegement 									
1.lost due to time out or disturbance									
2.recieved by sender
2
sends the acknowlegement that the next expected frame is 9..............
window of frames sent to reciever..............
error introduced
1.NO ERROR
2.ERROR introduced
1
window recieved at reciever side
window containing frame from 11 to 16 is recieved.........
 sends the ackcowlegement that the next expected frame is 17.............
window of frames sent to reciever..............
error introduced
1.NO ERROR
2.ERROR introduced
1
window recieved at reciever side
window containing frame from 15 to 20 is recieved.........
 sends the ackcowlegement that the next expected frame is 21.............

...............END OF TRANSMISSION...............

*/